<?php
namespace app\wendasns\service;

use app\wendasns\model\Article;
use app\wendasns\service\PwSrv;
use app\wendasns\model\Category;

use wendasns\Wend;
use think\Exception;

class PwArticle
{
	public $id = 0;
	
    public function __construct()
    {
        
    }
    
	//
	public function save()
	{
    	if(empty($this->id)){
    		return $this->add();
    	}else{
    		return $this->update();
    	}
	}
	
	//添加文章
	private function add()
	{
		//是否审核
    	$status = configure('wenda.postCheck') ? 'check' : 'normal';
    	$create_time = Wend::getTime();
    	
    	$data = [
    		'user_id' => $this->user_id,
    		'category_id' => $this->category_id,
    		'title' => $this->title,
    		'content' => $this->content,
    		'summary' => $this->summary,
    		'tags' => strtr($this->tags,[' '=>',']),
    		'speak' => $this->speak,
    		'ip' => request()->ip(),
    		'create_time' => $create_time,
    		'status' => $status,
    		'client' => Wend::getClient()
    	];

    	$dm = Article::create($data);
    	if(isset($dm->id)){
    		$dm->kid = getKid($dm->id, $dm->create_time);
    		$dm->save();
    		//设置标签
            PwSrv::setTags($dm->id, $dm->tags, 'article');

    		//更新用户数据
    		$dm->users()->find()->datas()->inc('article')->update();
    		
    		//记录用户行为
    		$dm->users()->find()->behaviors()->save(['source_id'=>$dm->id,'action'=>'article','create_time'=>$create_time]);
    		
    		//钩子
    		event('Article', ['aid'=>$dm->id]);
    		$this->id = $dm->id;
    		return $dm;
    	}else{
    		throw new Exception('发布失败');
    	}
	}
	
	//更新文章
	private function update()
	{
    	$dm = Article::find($this->id);
    	if(!$dm || $dm->remove){
    		throw new Exception('文章不存在或已删除');
    	}

		if(request()->loginUser->uid<>$dm->user_id){
    		throw new Exception('请登录账号后再操作');
    	}
    	if($dm->status<>'normal'){
    		throw new Exception('文章审核中');
    	}

    	$status = configure('wenda.postCheck') ? 'check' : 'normal';
        $dm->category_id = $this->category_id;
        $dm->title = $this->title;
        $dm->content = $this->content;
        $dm->summary = $this->summary;
        $dm->tags = strtr($this->tags,[' '=>',']);
        $dm->speak = $this->speak;
        $dm->status = $status;
		$dm->update_time = Wend::getTime();
		$dm->save();
		//设置标签
        PwSrv::setTags($dm->id, $dm->tags, 'article');
		return $dm;
	}
	
	//
	public function remove()
	{
    	$dm = Article::find($this->id);
    	if(!$dm || $dm->remove){
    		throw new Exception('文章不存在或已删除');
    	}

		if(request()->loginUser->uid<>$dm->user_id){
    		throw new Exception('请登录账号后再操作');
    	}
    	$dm->remove = 1;
    	$dm->save();
    	//钩子
    	event('DelArticle', ['aid'=>$this->id]);
    	return $dm;
	}
	
    //获取文章列表
    public function get($type='new', $limit=10, $category='all', $page=1)
    {
		if($category=='all'){
			$mark = '>';
			$term = 0;
		}else{
			$dm = Category::where('dir_name', $category)->where('type', 'article')->find();
			if(!$dm){
				return [];
			}
			$mark = '=';
			$term = $dm['id'];
		}
		$sql = [
			'price' => [
			    ['reward','>',0]
			],
			'new' => [],
			'hot' => [],
			'top' => [
				['top','>',0]
			]
		];
		if($type!='top') $sql[$type][] = ['top', '=', 0];
		$sql[$type][] = ['category_id', $mark, $term];
		$order = $type=='hot'?'visits':'create_time';
		$dm = Article::where($sql[$type])->order($order, 'desc')->paginate(['list_rows'=>$limit,'page'=>$page,'query'=>['cname'=>$category,'type'=>$type,'_s'=>'wendasns/article/index']]);
		return $dm;
	}
}